import request from '@/utils/request'
import type {
  aclListRes,
  aclRecordsObj,
  addRoleData,
  loginFormData,
  loginRes,
  roleListRes,
  userInfoRes,
} from './type'
enum API {
  LOGIN_URL = '/admin/acl/index/login',
  USERINFO_URL = '/admin/acl/index/info',
  LOGOUT_URL = '/admin/acl/index/logout',
  ACLLIST_URL = '/admin/acl/user/',
  ACLUSER_ADD_URL = '/admin/acl/user/save',
  ACLUSER_UPDATE_URL = '/admin/acl/user/update',
  ACLROLE_LIST_URL = '/admin/acl/user/toAssign/',
  ADD_ROLE_URL = '/admin/acl/user/doAssignRole',
  DELETE_ROLE_URL = '/admin/acl/user/remove/',
  DELETE_ALL_URL = '/admin/acl/user/batchRemove',
}
export const reqLogin = (data: loginFormData) => request.post<any, loginRes>(API.LOGIN_URL, data)
export const reqUserInfo = () => request.get<any, userInfoRes>(API.USERINFO_URL)
export const reqLogout = () => request.post<any, any>(API.LOGOUT_URL)

export const reqAclList = (pager: number, limit: number, username?: string) => {
  if (username) {
    return request.get<any, aclListRes>(API.ACLLIST_URL + `${pager}/${limit}?username=${username}`)
  } else {
    return request.get<any, aclListRes>(API.ACLLIST_URL + `${pager}/${limit}`)
  }
}
export const reqAddOrUpdateUer = (data: aclRecordsObj) => {
  if (data.id) {
    return request.put<any, any>(API.ACLUSER_UPDATE_URL, data)
  } else {
    return request.post<any, any>(API.ACLUSER_ADD_URL, data)
  }
}
export const reqgetRoleList = (id: number) =>
  request.get<any, roleListRes>(API.ACLROLE_LIST_URL + `${id}`)
export const reqaddRole = (data: addRoleData) => request.post<any, any>(API.ADD_ROLE_URL, data)
export const reqdeleteRole = (id: number) => request.delete<any, any>(API.DELETE_ROLE_URL + `${id}`)
export const reqdeleteAllRole = (idList: number[]) =>
  request.delete<any, any>(API.DELETE_ALL_URL, { data: idList })
